﻿using System.ServiceModel;

using Coding4Fun.Cannon.Robot;

namespace Coding4Fun.Cannon.Wcf
{
	[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
	public class ShootService : IShootService
	{
		private const int TotalRelays = 2;
		#region Implementation of IShootService
		private static PhidgetRelay Relay
		{
			get { return _relay ?? (_relay = new PhidgetRelay()); }
		}
		private static PhidgetRelay _relay;
		private int FireIndex
		{
			get { return _fireIndex; }
			set
			{
				if (value % TotalRelays == 0)
					value = 0;

				_fireIndex = value;
			}
		}
		private int _fireIndex;
		public void Fire()
		{
			FireByIndex(FireIndex++);
		}

		public void FireByIndex(int index)
		{
			Relay.Open(index);
		}

		public void FireMultiple(int count)
		{
			if( count < TotalRelays)
				count = TotalRelays;

			for (var i = 0; i < count; i++)
				Fire();
		}

		public int GetFireIndex()
		{
			return FireIndex;
		}

		public void ResetFireIndex()
		{
			FireIndex = 0;
		}

		#endregion
	}
}
